package com.rabbit.demo.controller;

import com.rabbit.demo.OrderInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/orderPage")
    public String orderPage(){
        return "orderPage";
    }

    @RequestMapping("/payPage")
    public String payPage(@RequestParam String id){
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setId(id);
        orderInfo.setStatus(0);
        OrderInfo.map.put(id,orderInfo);
        rabbitTemplate.convertAndSend("ex.go","go",id);
        return "payPage";
    }

//    @RequestMapping("/paySuccess")
//    public String paySuccess(){
//        return "paySuccess";
//    }
//
//    @RequestMapping("/payFail")
//    public String payFail(){
//        return "payFail";
//    }

    @RequestMapping("/payProcess")
    @ResponseBody
    public String payProcess(@RequestParam String id){
        OrderInfo orderInfo = OrderInfo.map.get(id);
        if(orderInfo.getStatus() == 2){
            return "payFail";
        }else if(orderInfo.getStatus() == 0){
            orderInfo.setStatus(1);
            return "paySuccess";
        }else
            return "retry";
    }
}
